#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;

struct TreeNode 
{
	int val;
	int left;
	int right;
}tree[N];

void pre_order(int x) {
	cout << tree[x].val << " ";
	if(tree[x].left)
		pre_order(tree[x].left);
	if(tree[x].right)
		pre_order(tree[x].right);
}

void in_order(int x) {
	if (tree[x].left)
		in_order(tree[x].left);
	cout << tree[x].val << " ";
	if (tree[x].right)
		in_order(tree[x].right);
}

void post_order(int x) {
	if (tree[x].left)
		post_order(tree[x].left);
	if (tree[x].right)
		post_order(tree[x].right);
	cout << tree[x].val << " ";
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; i++) {
		int l, r; cin >> l >> r;
		tree[i].val = i;
		tree[i].left = l;
		tree[i].right = r;
	}
	pre_order(1);
	cout << '\n';
	in_order(1);
	cout << '\n';
	post_order(1);
	cout << '\n';
	return 0;
}